package cn.doitedu.sql;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class _12_WindowJoin {
    public static void main(String[] args) {

        Configuration cfg = new Configuration();
        cfg.setString("taskmanager.memory.network.max", "1gb");
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment( cfg);
        env.enableCheckpointing(1000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");

        //env.setParallelism(1);

        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        tenv.getConfig().set("table.exec.source.idle-timeout","1s");
        tenv.getConfig().set("table.exec.state.ttl","1 hour");


        // {"uid":1,"event_id":"face_recg","action_time":10000}
        tenv.executeSql(
                " create table face_log_kfk(                                      "+
                        "   uid int,                                            "+
                        " 	event_id string,                                    "+
                        " 	action_time bigint,                                 "+
                        " 	pt as proctime(),                                   "+
                        " 	rt as to_timestamp_ltz(action_time,3),              "+
                        " 	watermark for rt as rt                              "+
                        "  ) with (                                             "+
                        "    'connector' = 'kafka',                             "+
                        "    'topic' = 'face-log',                            "+
                        "    'properties.bootstrap.servers' = 'doitedu:9092',   "+
                        "    'properties.group.id' = 'g003',                    "+
                        "    'scan.startup.mode' = 'latest-offset',             "+
                        "    'value.format' = 'json',                           "+
                        "    'value.fields-include' = 'EXCEPT_KEY'              "+
                        " )                                                     "
        );

        // {"uid":1,"face_status":1,"ts":11000}
        tenv.executeSql(
                " create table face_res_kfk(                                    "+
                        "   uid int,                                            "+
                        " 	face_status int,                                    "+
                        " 	ts bigint,                                          "+
                        " 	pt as proctime(),                                   "+
                        " 	rt as to_timestamp_ltz(ts,3),                       "+
                        " 	watermark for rt as rt                              "+
                        "  ) with (                                             "+
                        "    'connector' = 'kafka',                             "+
                        "    'topic' = 'face-res',                            "+
                        "    'properties.bootstrap.servers' = 'doitedu:9092',   "+
                        "    'properties.group.id' = 'g003',                    "+
                        "    'scan.startup.mode' = 'latest-offset',             "+
                        "    'value.format' = 'json',                           "+
                        "    'value.fields-include' = 'EXCEPT_KEY'              "+
                        " )                                                     "
        );

        tenv.executeSql(
                " WITH tmp1 AS (                                                                  "+
                        "     SELECT                                                                      "+
                        "         uid,                                                                    "+
                        "         event_id,                                                               "+
                        "         action_time,                                                            "+
                        "         window_start,                                                           "+
                        "         window_end                                                              "+
                        "     FROM TABLE(                                                                 "+
                        "         TUMBLE(TABLE face_log_kfk, DESCRIPTOR(rt),INTERVAL '10' SECOND)         "+
                        "     )                                                                           "+
                        " ),                                                                              "+
                        " tmp2 AS (                                                                       "+
                        "     SELECT                                                                      "+
                        "         uid,                                                                    "+
                        "         face_status,                                                            "+
                        "         ts,                                                                     "+
                        "         window_start,                                                           "+
                        "         window_end                                                              "+
                        "     FROM TABLE(                                                                 "+
                        "         TUMBLE(TABLE face_res_kfk, DESCRIPTOR(rt),INTERVAL '10' SECOND)         "+
                        "     )                                                                           "+
                        " )                                                                               "+
                        "                                                                                 "+
                        "                                                                                 "+
                        " SELECT                                                                          "+
                        "     tmp1.uid,                                                                   "+
                        "     tmp1.event_id,                                                              "+
                        "     tmp1.action_time,                                                           "+
                        "     tmp1.window_start,                                                          "+
                        "     tmp1.window_end,                                                            "+
                        "     tmp2.face_status,                                                           "+
                        "     tmp2.ts                                                                     "+
                        " FROM tmp1 JOIN tmp2                                                             "+
                        " ON tmp1.window_start = tmp2.window_start                                        "+
                        " AND tmp1.window_end = tmp2.window_end                                           "+
                        " AND tmp1.uid = tmp2.uid                                                         "
        ).print();



    }
}
